Observables and ViewModels
BaseObservable
BaseObservable
is the class that makes this observable behavior possible.
BaseViewModel
BaseViewModel
is a specialized BaseObservable
subclass. You will extend this class to create your view models. These are referred to as VMs or ViewModels, which is where Model View View Model pattern gets it's name.
We use viewModels like any other class, invoking methods, and setting properties.
autogeneration of code
Maestro framework applications use the maestro-roku-bsc-plugin to generate their code for them. This saves error-prone confounding boilerplate code from having to be written. A typical maestro view might generate code as such:
'generated by maestro-bsc-plugin
function init()
m_createNodeVars()
end function
function m_createNodeVars()
for each id in [ "refreshTimer","backgroundPoster","emailTitle","emailButton","passwordTitle","passwordButton","signInButton","loadingIndicator"]
m[id] = m.top.findNode(id)
end for
end function
function m_createVM()
m.vm = screens_LoginScreenVM()
m.vm.initialize()
mx_initializeBindings()
end function
function m_initBindings()
if m.vm <> invalid then
vm = m.vm
vm.bindField("refreshTimerDuration", m.refreshTimer, "duration", true, invalid, false)
vm.bindField("refreshTimerControl", m.refreshTimer, "control", true, invalid, false)
vm.observeNodeField(m.refreshTimer, "fire", vm.onRefreshTimerFire, "none", false)
vm.bindField("emailText", m.emailButton, "text", true, invalid, false)
vm.bindField("emailButtonStyle", m.emailButton, "style", true, invalid, false)
vm.bindField("passwordText", m.passwordButton, "text", true, invalid, false)
vm.bindField("passwordButtonStyle", m.passwordButton, "style", true, invalid, false)
vm.bindField("isLoading", m.loadingIndicator, "isActive", true, invalid, false)
if vm.onBindingsConfigured <> invalid
vm.onBindingsConfigured()
end if
end if
end function
function m_initStaticBindings()
if m.vm <> invalid then
vm = m.vm
end if
end function
Observers and bindings
These are the 2 forms of observable interaction that the base classes provide:
- observeField: this will call back a function, when an observable (i.e ViewModel) field is set. This is like
observeField
for BrightScript nodes. - bindField: this will bind the value of a field to a field on a node.
- bindNodeField: this will bind the value of a field, to a field on an observer (i.e. ViewModel). The target field can also be a function, in which case it will be invoked with the bound fields' value.
Wiring up bindings in code is discouraged
We can wire up bindings manually; but it is best that you use xml bindings as discussed in the docs.